package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.ManualErrAdjustTxnMapper;
import cc.rengu.igas.bomp.common.entity.ErrAdjustTxn;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 差错调账流水表
 * Created by xubh on 2020/6/3
 */
public class ManualErrAdjustTxnMapperImpl implements ManualErrAdjustTxnMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);


    @Override
    public int insertErrAdjustTxn(ErrAdjustTxn errAdjustTxn) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertErrAdjustTxn");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, errAdjustTxn, null);
        if (0 != iReturnCode) {
            rglog.error("登记差错调账流水失败,instId:<{}>,primaryKey:<{}>,iReturnCode:<{}>", errAdjustTxn.getInstId(), errAdjustTxn.getPrimaryKey(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateManualErrAdjustTxnByprimaryKey(ErrAdjustTxn errAdjustTxn) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateManualErrAdjustTxnByprimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, errAdjustTxn, null);
        if (0 != iReturnCode) {
            rglog.error("更新差错调账流水失败,instId:<{}>,primaryKey:<{}>,iReturnCode:<{}>", errAdjustTxn.getInstId(), errAdjustTxn.getPrimaryKey(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ErrAdjustTxn selectManualErrAdjustTxnByprimaryKey(String acctUnionKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectManualErrAdjustTxnByprimaryKey");
        ErrAdjustTxn errAdjustTxn = new ErrAdjustTxn();
        errAdjustTxn.setPrimaryKey(acctUnionKey);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, errAdjustTxn, errAdjustTxn);
        if (0 != iReturnCode) {
            rglog.error("获取差错调账流水失败,primaryKey:<{}>", acctUnionKey);
            return null;
        }
        return errAdjustTxn;
    }

    @Override
    public ErrAdjustTxn selectManualErrAdjustTxnByprimaryKeyNew(String acctUnionKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectManualErrAdjustTxnByprimaryKeyNew");
        ErrAdjustTxn errAdjustTxn = new ErrAdjustTxn();
        errAdjustTxn.setPrimaryKey(acctUnionKey);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, errAdjustTxn, errAdjustTxn);
        if (0 != iReturnCode) {
            rglog.error("获取差错调账流水失败,primaryKey:<{}>", acctUnionKey);
            return null;
        }
        return errAdjustTxn;
    }


}
